<template>
<!--面包屑-->
<div style="margin-bottom: 30px;">
  <el-breadcrumb separator="/">
    <el-breadcrumb-item :to="{ path: '/Main' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>
      <a href="/RoleModule/ShowRoleList">角色管理</a>
    </el-breadcrumb-item>
    <el-breadcrumb-item>角色列表展示</el-breadcrumb-item>
  </el-breadcrumb>
</div>

  <el-button type="primary" @click="router.push('AddRole')">新增</el-button>&nbsp;&nbsp;
  <!--角色列表-->
  <div>
    <el-table :data="roleInfoData" border style="width: 100%">
      <el-table-column prop="id" label="编号" align="center" />
      <el-table-column prop="name" label="角色名称" align="center" />
      <el-table-column prop="role_type" label="角色类型" align="center" />
      <el-table-column prop="data_scope" label="数据范围" align="center">
        <template #default="scope">
          <span v-if="scope.row.data_scope == 1">全部数据</span>
          <span v-else-if="scope.row.data_scope == 2">公司已下数据</span>
          <span v-else-if="scope.row.data_scope == 8">个人数据</span>
        </template>
      </el-table-column>
      <el-table-column prop="role_level" label="角色等级" align="center">
        <template #default="scope">
          <span v-if="scope.row.role_level == 0">0级</span>
          <span v-else-if="scope.row.role_level == 1">1级</span>
          <span v-else-if="scope.row.role_level == 2">2级</span>
          <span v-else-if="scope.row.role_level == 3">3级</span>
          <span v-else-if="scope.row.role_level == 4">4级</span>
          <span v-else-if="scope.row.role_level == 5">5级</span>
          <span v-else-if="scope.row.role_level == 6">6级</span>
          <span v-else-if="scope.row.role_level == 7">7级</span>
          <span v-else-if="scope.row.role_level == 8">8级</span>
          <span v-else-if="scope.row.role_level == 9">9级</span>
          <span v-else-if="scope.row.role_level == 10">10级</span>
        </template>
      </el-table-column>
      <el-table-column prop="useable" label="是否可用" align="center">
        <template v-slot="scope">
          <span v-if="scope.row.useable == 1">启用</span>
          <span v-else-if="scope.row.useable == 0">禁用</span>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="create_by" label="创建人" align="center" /> -->
      <el-table-column prop="create_date" label="创建时间" align="center" >
        <template v-slot="scope">
          {{ scope.row.create_date.replace("T", " ").substring(0, 19) }}
        </template>
      </el-table-column>
      <!-- <el-table-column prop="update_by" label="修改人" align="center" /> -->
      <el-table-column prop="update_date" label="修改时间" align="center" />
      <el-table-column label="操作" align="center" width="220px" >
        <template v-slot="scope">
          <el-button type="primary" @click="CheckEditor(scope.row.id)">编辑</el-button>
          <el-button type="primary" @click="assignPersonnel(scope.row.id)">分配人员</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>

  <!--分页-->
  <div>
    <table>
      <thead></thead>
      <tbody>
        <tr>
          <td>
            <el-pagination v-model:current-page="form.pageIndex" v-model:page-size="form.pageSize"
              :page-sizes="[3, 5, 7, 9]" :background="true" layout="total,slot, sizes" :total="form.totalCount"
              prev-text="上一页" next-text="下一页" @size-change="handleSizeChange" @current-change="handleCurrentChange">
              <span>
                ，每页{{ form.pageSize }}条，当前第:{{ form.pageIndex }}/{{ form.totalPage }}页
              </span>
            </el-pagination>
          </td>

          <td>
            <el-pagination v-model:current-page="form.pageIndex" v-model:page-size="form.pageSize"
              :page-sizes="[3, 5, 7, 9]" :background="true" layout="slot, prev, pager, next" :total="form.totalCount"
              prev-text="上一页" next-text="下一页" @size-change="handleSizeChange" @current-change="handleCurrentChange">
              <el-button type="primary" class="btn-prev" @click="handleCurrentChange(1)"
                :disabled="form.pageIndex == 1">首页</el-button>
            </el-pagination>
          </td>

          <td>
            <el-pagination v-model:current-page="form.pageIndex" v-model:page-size="form.pageSize"
              :page-sizes="[3, 5, 7, 9]" :background="true" layout="slot, jumper" :total="form.totalCount"
              prev-text="上一页" next-text="下一页" @size-change="handleSizeChange" @current-change="handleCurrentChange">
              <el-button type="primary" class="btn-prev" @click="handleCurrentChange(form.totalPage)"
                :disabled="form.pageIndex == form.totalPage">尾页</el-button>
            </el-pagination>
          </td>
        </tr>
      </tbody>
    </table>
  </div>

  <el-dialog v-model="dialogFormVisible" title="人员分配" width="500px">
    <el-form :model="form">
      <el-select v-model="addUserRole.userId" multiple placeholder="请选择用户" style="width: 240px">
        <el-option v-for="item in UserSelectData" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="addUserRoles" type="primary">保存</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">取消</el-button>
      </div>
    </template>
  </el-dialog>


</template>

<script setup lang="ts">
import { reactive, ref, onMounted, computed } from 'vue'
import axios from 'axios';
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
import { ElMessage, ElMessageBox } from 'element-plus'

import { useRoute, useRouter } from 'vue-router'

const route = useRoute();
const router = useRouter();

onMounted(() => {
  getRoleInfoList();
})

const form: any = reactive({
  pageIndex: 1,
  pageSize: 5,
  totalPage: 0,
  totalCount: 0,
})

const Data: any = ref([])

//角色列表
const roleInfoData: any = ref([])
const getRoleInfoList = () => {

  axios.get('http://1.117.93.87:8000/rbacdu/api/Role/show', {
    params: {
      pageIndex: form.pageIndex,
      pageSize: form.pageSize,
    }
  }).then(res => {
    console.log(res);
    roleInfoData.value = res.data.data.list;
    form.totalPage = res.data.data.pageCount;
    form.totalCount = res.data.data.totalCount;
  }).catch(err => {
    console.log(err);

  })
}
//分页
const handleSizeChange = (val: number) => {
  form.pageSize = val;
  getRoleInfoList()
}
const handleCurrentChange = (val: number) => {
  form.pageIndex = val;
  getRoleInfoList()
}

//用户列表
const UserSelectData:any=ref([])
const getUserSelect=()=>{
  axios.get('http://1.117.93.87:8000/rbacdu/api/User/UserSelectShow').then(res=>{
    // console.log(res);
    UserSelectData.value = res.data.data
  }).catch(err=>{
    console.log(err);
    
  })    
}
//分配人员 
const addUserRole=reactive({
  userId:[],
  roleId:'',
})
const dialogFormVisible = ref(false)
const assignPersonnel = (id: any) => {
  dialogFormVisible.value = true;
  getUserSelect();
  addUserRole.roleId = id;
}

//提交分配人员
const addUserRoles=()=>{
  console.log(addUserRole);
  axios.post("http://1.117.93.87:8000/rbacxie/api/Role/UserRoleCreate",addUserRole)
  .then(res=>{
    if(res.data.code==200){
      ElMessage.success("添加成功");
      dialogFormVisible.value = false;
    }
    else{
      ElMessage.error("添加失败");
    }
  })
  .catch(err=>{
    console.log(err);
  })
}

//查看详情
const CheckEditor = (id:number) => {
  router.push({
    name: '/ReverseRole',
    query:{
      id:id
    }
  })
}


</script>

<style scoped></style>